package org.spring.jruby.erbtest.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.spring.jruby.erbtest.core.Entry;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;

/**
 *
 * @author oleg
 */
@Controller
public class TestController {

    @RequestMapping("index.htm")
    private ModelAndView handleRequest(final String[] n) {
        final Map<String, Object> model = new HashMap<>();
        final List<Entry> entries = new ArrayList<>();
        final String[] names = n == null ? new String[]{"Foo", "Bar", "FooBar", "BarFoo"} : n;
        final Random rnd = new Random();
        for (int i = 0; i < names.length; i++) {
            final Entry e = new Entry();
            e.setName(names[i]);
            e.setValue(String.valueOf(rnd.nextDouble()));
            entries.add(e);
        }
        model.put("entries", entries);
        model.put("title", "Hello ERB!");
        return new ModelAndView("index", model);
    }
}
